package com.example.provider.javastudy.invokedynamic;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

import static java.lang.invoke.MethodHandles.lookup;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @Package com.example.demo.invokedynamic
 * @author: 红雪（hongxue@maihaoche.com）
 * @date: 2019/1/14 1:54 PM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 * 注意：本内容仅限于卖好车内部传阅，禁止外泄以及用于其他的商业项目
 */
public class MethodHandleTest {


    static class ClassA{

        public void println(String s){
            System.out.println(s);
        }
    }

    public static void main(String[] args) throws Throwable {
        Object object = System.currentTimeMillis() % 2 ==0 ?System.out:new ClassA();
        getPrintlnPM(object).invokeExact("durui");

    }
    private static MethodHandle getPrintlnPM(Object recever) throws NoSuchMethodException, IllegalAccessException {
        MethodType mt = MethodType.methodType(void.class,String.class);

        return lookup().findVirtual(recever.getClass(),"println",mt).bindTo(recever);
    }
}
